package com.imooc.ad.index.interest;

import com.imooc.ad.index.IndexAware;
import com.imooc.ad.utils.CommonUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentSkipListSet;

@Slf4j
@Component
public class UnitItIndex implements IndexAware<String,Set<Long>> {


    private static Map<String,Set<Long>> itUnitMap;
    private static Map<Long,Set<String>> unitItMap;

    static {
        itUnitMap = new ConcurrentHashMap<>();
        unitItMap = new ConcurrentHashMap<>();

    }


    @Override
    public Set<Long> get(String key) {
        return itUnitMap.get(key);
    }

    @Override
    public void add(String key, Set<Long> value) {
        log.info("UnitItIndex, before add: {}", unitItMap);
            
        Set<Long>unitIds = CommonUtils.getorCreate(key,itUnitMap,ConcurrentSkipListSet::new);
        unitIds.addAll(value);
        
        value.forEach(unitId->{
            Set<String> its = CommonUtils.getorCreate(unitId, unitItMap, ConcurrentSkipListSet::new);
            its.add(key);
        });
        
        
        
        log.info("UnitItIndex, after add: {}", unitItMap);

    }

    @Override
    public void update(String key, Set<Long> value) {
        log.error("it index can not support update");
    }

    @Override
    public void delete(String key, Set<Long> value) {
        log.info("UnitItIndex, before delete: {}", unitItMap);
        // 删除 it 对应的 unitIds
        Set<Long> unitIds = CommonUtils.getorCreate(key,itUnitMap,ConcurrentSkipListSet::new);
        unitIds.removeAll(value);
        // 删除 unitId 对应的 its
        value.forEach(unitId->{
            Set<String> itTagSet = CommonUtils.getorCreate(unitId, unitItMap, ConcurrentSkipListSet::new);
            itTagSet.remove(key);
        });

        log.info("UnitItIndex, after delete: {}", unitItMap);
    }

    public boolean match(Long unitId, List<String> itTags){

        if (unitItMap.containsKey(unitId)&&CollectionUtils.isNotEmpty(unitItMap.get(unitId))){
            Set<String> unitItTags = unitItMap.get(unitId);
            return CollectionUtils.isSubCollection(itTags,unitItTags);
        }
        return false;
    }



}
